package com.wy.commandUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ExecuteCommandUtil {
    /**
     * 执行Maven命令的通用方法
     */
    public static void executeCommand(ProcessBuilder processBuilder) throws IOException, InterruptedException {
        // 重定向错误流到标准输出
        processBuilder.redirectErrorStream(true);
        // 启动进程
        Process process = processBuilder.start();

        // 读取输出
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        }
        // 等待进程完成
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            throw new RuntimeException("Maven 命令执行失败，退出码: " + exitCode);
        }
    }
}
